#include "ShelfSprite.h"
#include "CardSprite.h"
#include "BellSprite.h"
#include "AudioManage.h"

static const char* CONST_PNG_SHELF = "Shelf/shelf.png";
static const char* CONST_PNG_REST_BUTTON_N = "Shelf/button_reset_N.png";
static const char* CONST_PNG_REST_BUTTON_S = "Shelf/button_reset_S.png";
static const char* CONST_PNG_RIGHT = "Cups/right.png";
static const char* CONST_PNG_WRONG = "Cups/wrong.png";

ShelfSprite::ShelfSprite()
{
	this->create();
	this->initWithFile(CONST_PNG_SHELF);

	float width = this->getContentSize().width;;
	float height = this->getContentSize().height;

	for (int i = 0; i < 5; i++)
	{
		Positions[i] = Vec2(width / 6 * (i + 1), height / 2);
		Cups[i] = new CupSprite(i);
		Cups[i]->setScale(1.6f);
		Cups[i]->setPosition(Positions[i]);
		this->addChild(Cups[i]);

		keys[i] = -1;
		keyIndex = 0;
	}	

	Size visibleSize = Director::getInstance()->getVisibleSize();

	for (int i = 0; i < 5; i++)
	{
		this->CrossPosition[i] = Vec2(visibleSize.width / 6 * (i + 1), visibleSize.height / 2);
		this->VerticalPosition[i] = Vec2(visibleSize.width / 2, visibleSize.height * ( 0.4f + 0.03f * i));
	}

	resetButton = MenuItemImage::create(CONST_PNG_REST_BUTTON_N, CONST_PNG_REST_BUTTON_S,
		CC_CALLBACK_0(ShelfSprite::resetButtonCallBack,this));

	resetButton->setPosition(Vec2(width * 0.85f, height * 1.2f));
	resetButton->setScale(1.3f);

	Menu* shelfMenu = Menu::create(resetButton, NULL);
	shelfMenu->setPosition(Vec2::ZERO);
	
	ShelfSprite::right = Sprite::create(CONST_PNG_RIGHT);	
	right->setScale(3.0f);
	right->setVisible(false);
	this->addChild(right, 6);

	ShelfSprite::wrong = Sprite::create(CONST_PNG_WRONG);
	wrong->setScale(3.0f);
	wrong->setVisible(false);
	this->addChild(wrong, 6);


	this->addChild(shelfMenu);
}

ShelfSprite::~ShelfSprite()
{
	for (int i = 0; i < 5; i++)
	{
		delete Cups[i];
		Cups[i] = NULL;
	}
}

ShelfSprite* ShelfSprite::thisSprite = nullptr;
Sprite* ShelfSprite::right = nullptr;
Sprite* ShelfSprite::wrong = nullptr;

ShelfSprite* ShelfSprite::getInstance()
{
	if (nullptr == ShelfSprite::thisSprite)
	{
		ShelfSprite::thisSprite = new ShelfSprite();
	}
	return thisSprite;
}

bool ShelfSprite::putACup(CupSprite* cup)
{
	int i = thisSprite->keyIndex;

	if (i > 4)
	{
		return false;			 
	}

	thisSprite->keys[i] = cup->myColor;
	thisSprite->keyIndex += 1;

	if (CardSprite::crossCard)
	{
		cup->setPosition(thisSprite->convertToNodeSpace(thisSprite->CrossPosition[i]));
		cup->setLocalZOrder(i);
	}
	else
	{
		cup->setPosition(thisSprite->convertToNodeSpace(thisSprite->VerticalPosition[i]));
		cup->setLocalZOrder(i);
	}

	return true;
}

void ShelfSprite::reSetCup()
{
	for (int i = 0; i < 5; i++)
	{
		Cups[i]->setPosition(Positions[i]);
		Cups[i]->myState = inShelf;
	}

	for (int i = 0; i < 5; i++)
	{
		keys[i] = -1;
	}

	keyIndex = 0;
}

void ShelfSprite::resetButtonCallBack()
{
	AudioManage::button2Sound();
	reSetCup();
}

void ShelfSprite::keysRight()
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	right->setPosition(thisSprite->convertToNodeSpace(Vec2(visibleSize.width / 2, visibleSize.height / 2)));

	thisSprite->right->runAction(
		Sequence::create(
		FadeOut::create(0.0f),
		Show::create(),
		FadeIn::create(0.2f),
		DelayTime::create(0.6f),
		FadeOut::create(0.2f),
		Hide::create(),
		DelayTime::create(0.2f),
		CallFunc::create([]{
		ShelfSprite::getInstance()->reSetCup(); 
		BellSprite::enableBell();
		}),
		NULL
		));
}

void ShelfSprite::keysWrong()
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	wrong->setPosition(thisSprite->convertToNodeSpace(Vec2(visibleSize.width / 2, visibleSize.height / 2)));

	if (thisSprite->wrong->isVisible())
	{
		thisSprite->wrong->setVisible(false);
	}

	thisSprite->wrong->runAction(
		Sequence::create(
		Show::create(),
		DelayTime::create(0.4f),
		Hide::create(),
		NULL
		));
}

void ShelfSprite::disposeSprite()
{
	//delete thisSprite;
	thisSprite = nullptr;
}
